import { modelHttp } from "@/util/httpClient/ModelNodeHttpClient";
import { BreakpointTriggerType } from "@/engine/BreakpointsService";
import { controller } from './Controller';

class ModelApi {

  constructor() {}

  pluginSiminstall() {
    return modelHttp.get({
      api: 'plugin/siminstall',
    });
  }

  modelInfo(p: ModelInfoParam) {
    return modelHttp.get({
      api: 'model/info',
      data: p
    });
  }

  modelInfos(p: { projectId: string, modelIds: string[] }) {
    return modelHttp.post({
      api: 'model/infos',
      data: p
    });
  }

  modelPropertyListByKeys(p: ModelPropertyListByKeysParam) {
    return modelHttp.post({
      api: 'model/property/listByKeys',
      data: p
    });
  }

  async modelPropertyByKey(p: ModelPropertyByKeyParam) {
    const { data } = await modelHttp.post({
      api: 'model/property/listByKeys',
      data: {
        ...p,
        keys: [p.key]
      }
    });
    return data?.data.length ? data.data[0] : undefined;
  }

  modelPropertyGetOwnedLiteral(p: ModelInfoParam) {
    return modelHttp.get({
      api: 'model/property/getOwnedLiteral',
      data: p
    });
  }

  systemMatlabConfig() {
    return modelHttp.get({
      api: 'system/matlabConfig',
    });
  }

  projectGetSimulationConfig(projectId: string) {
    return modelHttp.get({
      api: 'project/getSimulationConfig',
      data: { projectId }
    });
  }

  getProjectInfo(projectId: string) {
    return modelHttp.get({
      api: 'project/info',
      data: { projectId }
    });
  }

  modelSimulationQueryPackages(projectId: string) {
    return modelHttp.get({
      api: 'model/simulation/queryPackages',
      data: { projectId }
    });
  }

  modelSimulationQueryInstancesByClassifier(p: { projectId: string, classifierId: string }) {
    return modelHttp.get({
      api: 'model/simulation/queryInstancesByClassifier',
      data: p
    });
  }

  modelGetBreakpointList() {
    return modelHttp.get({
      api: 'model/getBreakpointList',
      data: { projectId: controller.projectId }
    });
  }

  modelAddBreakpoint(p: ModelInfoParam) {
    return modelHttp.post({
      api: 'model/addBreakpoint',
      data: p
    });
  }

  modelDeleteBreakpoint(p: { projectId: string, modelIds: string[] }) {
    return modelHttp.post({
      api: 'model/deleteBreakpoint',
      data: p
    });
  }

  modelDeleteAllBreakpoint() {
    return modelHttp.post({
      api: 'model/deleteAllBreakpoint',
      data: { projectId: controller.projectId }
    });
  }

  modelEditBreakpoint(p: ModelEditBreakpointParam) {
    return modelHttp.post({
      api: 'model/editBreakpoint',
      data: p
    });
  }

  modelPropertyList(p: ModelInfoParam) {
    return modelHttp.get({
      api: 'model/property/list',
      data: p
    });
  }

  modelPropertyGetPropertyValuesById(p: ModelPropertyGetPropertyValuesByIdParam) {
    return modelHttp.post({
      api: 'model/property/getPropertyValuesById',
      data: p
    });
  }

  modelCheckElementSupportEmulation(p: ModelCheckElementSupportEmulationParam) {
    return modelHttp.get({
      api: 'model/checkElementSupportEmulation',
      data: p
    });
  }

  shapeQueryCuieShapeTree(p: ShapeQueryCuieShapeTreeParam) {
    return modelHttp.get({
      api: 'shape/queryCuieShapeTree',
      data: p
    });
  }

  async getChildren(projectId: string, modelId: string) {
    const res = await modelHttp.get({
      api: 'model/children',
      data: { projectId, modelId }
    });

    return res.data.data;
  }

}

export default ModelApi;

export type ModelInfoParam = {
  projectId: string
  modelId: string
}

export type ModelEditBreakpointParam = {
  projectId: string
  modelId: string
  editField: "condition" | "enabled" | "suspend"
  value: string | boolean | BreakpointTriggerType
}

export type ModelPropertyGetPropertyValuesByIdParam = {
  projectId: string
  modelId: string
  propertyKeys: string[]
}

export type ShapeQueryCuieShapeTreeParam = {
  projectId: string
  modelId: string
}

export type ModelCheckElementSupportEmulationParam = {
  projectId: string
  modelId: string
}

export type ModelPropertyListByKeysParam = {
  projectId: string
  modelId: string
  keys: string[]
}

export type ModelPropertyByKeyParam = {
  projectId: string
  modelId: string
  key: string
}

